package com.android.tea.shopper;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.android.tea.R;
import com.android.tea.TeaEncyclopediaApp;
import com.android.tea.api.ApiResult;
import com.android.tea.api.ApiReturnResultListener;
import com.android.tea.api.TeaApi;
import com.android.tea.entity.TeaCompany;

public class NewShopperActivity extends Activity {

	ImageButton ibnewBack;
	Button buttonOk;
	EditText new_name, new_class, new_addr, new_phone, new_email, new_cost;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_shopper);
		TeaEncyclopediaApp.list.add(this);
		initComponents();

	}

	private void initComponents() {
		ibnewBack = (ImageButton) findViewById(R.id.btn_new_back);
		buttonOk = (Button) findViewById(R.id.btn_new_ok);
		new_name = (EditText) findViewById(R.id.new_name);
		new_class = (EditText) findViewById(R.id.new_class);
		new_addr = (EditText) findViewById(R.id.new_addr);
		new_phone = (EditText) findViewById(R.id.new_phone);
		new_email = (EditText) findViewById(R.id.new_email);
		new_cost = (EditText) findViewById(R.id.new_cost);
		ibnewBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		buttonOk.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (isEmpty(new_name) || isEmpty(new_class)
						|| isEmpty(new_addr)) {
					Toast.makeText(NewShopperActivity.this, "名称，茶系，地址必须填写",
							Toast.LENGTH_LONG).show();
					return;
				}
				TeaApi teaapi = new TeaApi(NewShopperActivity.this);

				TeaCompany company = new TeaCompany();
				company.setCompanyName(new_name.getText().toString());
				company.setCoinId(new_class.getText().toString());
				company.setCompanyCaco(new_cost.getText().toString());
				company.setCompanyEmail(new_email.getText().toString());
				company.setCompanyMap(new_addr.getText().toString());
				company.setCompanyTel(new_phone.getText().toString());
				teaapi.addTeaShop(0, company);
				ApiReturnResultListener returnResultListener = new ApiReturnResultListener() {

					@Override
					public <T> void onReturnSucceedResult(int requestCode,
							ApiResult<T> apiResult) {
						Toast.makeText(NewShopperActivity.this, "添加成功",
								Toast.LENGTH_LONG).show();
						finish();
					}

					@Override
					public <T> void onReturnFailResult(int requestCode,
							ApiResult<T> apiResult) {
						Toast.makeText(NewShopperActivity.this, "添加失败，请稍后再试",
								Toast.LENGTH_LONG).show();

					}
				};
				;

				teaapi.setReturnResultListener(returnResultListener);
			}
		});
	}

	private boolean isEmpty(EditText et) {

		if (et != null && et.getText().length() > 0) {
			return false;
		}
		return true;
	}
}
